package com.yonyou.brigade.cron.order.huazhu;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.cron.util.SendMailUtil;
import com.yonyou.brigade.third_parties.huazhu.service.IHzOrderService;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author houyb
 */
@Component
@ElasticJobConfig(cron = "0 30 1 * * ? ", shardingTotalCount = 1)
public class FetchHuaZhuOrders implements SimpleJob {

    private static final Logger logger = LoggerFactory.getLogger(FetchHuaZhuOrders.class);

    @Autowired
    private IHzOrderService hzOrderService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    private SendMailUtil sendMailUtil;


    @Override
    public void execute(ShardingContext shardingContext) {
        logger.info("<<<<HuaZhu-hotel-pull-orders-begin>>>>");
        String begin = DateUtils.getCurrentDateMinusOneDay();
        String end = DateUtils.getCurrentDateMinusOneDay();
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.HUAZHU.getName());
        if(!CollectionUtils.isEmpty(briChannels)) {
            for (BriChannel briChannel : briChannels) {
                String tenant = briChannel.getTenant();
                String orgpk = briChannel.getOrgpk();
                try {
                    hzOrderService.getOrders(tenant, "", begin, end, briChannel);
                } catch (Exception e) {
                    sendMailUtil.sendMailToDeveloper("环境华住酒店拉单失败", e.getMessage());
                }
            }
            logger.info("<<<<HuaZhu-hotel-pull-orders-end>>>>");
        }
    }

}

